package com.dotflat.cookbook.controller;

import com.dotflat.cookbook.service.TongBangCookbookService;
import com.matt.commons.dto.sso.AreaDTO;
import com.matt.commons.model.StatusResponse;
import com.matt.service.remoteCall.SsoCallService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.matt.foundation.constant.RequestConstant.TICKET;

@RestController
@RequestMapping("/api/tongbang")
@Slf4j
@Api(tags = "童帮 - 食谱")
public class TongBangCookbookController {

    @Autowired
    private TongBangCookbookService tongBangCookbookService;

    @Autowired
    private SsoCallService ssoCallService;

    @ApiOperation(value = "园区信息创建带量食谱")
    @PostMapping("/create")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = String.class),
    })
    public StatusResponse tongBangAreaCreate(@RequestHeader(TICKET) String ticket) {
        AreaDTO area = ssoCallService.getAreaDTO(ticket);
        tongBangCookbookService.tongbangAreaCreate(ticket, area );
        return StatusResponse.success("");
    }

    @ApiOperation(value = "园区是否已经创建带量食谱")
    @PostMapping("/iscreated")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = String.class),
    })
    public StatusResponse isCookbookAreaCreated(@RequestHeader(TICKET) String ticket) {
        AreaDTO area = ssoCallService.getAreaDTO(ticket);
        boolean result = tongBangCookbookService.isTongBangAreaSynced(ticket, area.getId());
        return StatusResponse.success(result);
    }

    @ApiOperation(value = "食谱URL")
    @PostMapping("/auth_url")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = String.class),
    })
    public StatusResponse cookBookURL(@RequestHeader(TICKET) String ticket) throws  Exception{
        AreaDTO area = ssoCallService.getAreaDTO(ticket);
        String url = tongBangCookbookService.cookBookURL(ticket, area);
        return StatusResponse.success(url);
    }

}
